/* $Id: ProgramListActivity.java 89 2008-01-22 02:38:52Z osborn.steven $
 * 
 * Undroid Package Manager
 *
 * Copyright 2007 Steven Osborn
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.bitsetters.undroid;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;

import com.bitsetters.android.common.BulletedText;
import com.bitsetters.android.common.BulletedTextListAdapter;
import com.bitsetters.undroid.common.CommonMenu;
import com.bitsetters.undroid.common.Footer;
import com.bitsetters.undroid.helpers.DbHelper;

/**
 * Displays a list of applications 
 * 
 * @author Steven Osborn - http://steven.bitsetters.com
 */
public class ProgramListActivity extends ListActivity {
    
    public static List<BulletedText> items;
    public static String APP_ID = "app_id";
    private DbHelper db;

    /**
     * 
     */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        db = new DbHelper(this);
        
        setContentView(R.layout.list);

        int catid = 0;
        
        Bundle extras = getIntent().getExtras();            
        catid = extras != null ? extras.getInteger(CategoryListActivity.KEY_CATID) : null;

        
        items = getListItems(catid);

        BulletedTextListAdapter btla = new BulletedTextListAdapter(this);
        btla.setListItems(items);
        setListAdapter(btla);
        
        Footer.setup(this);
    }
    
    /**
     * 
     * @param catid
     * @return
     */
    private List<BulletedText> getListItems(int catid) {
        List<AppEntry> raw_list = db.getCategoryPrograms(catid);
        List<BulletedText> bl = new ArrayList<BulletedText>();
        for(int i=0; i< raw_list.size(); i++) {
            BulletedText b = null;
            b = new BulletedText(raw_list.get(i).label,
                    getResources().getDrawable(R.drawable.category_other22),
                    raw_list.get(i).summary);
            b.setDataObject(raw_list.get(i));
            bl.add(b);
        }
        return bl;
    }
    
    /**
     * 
     */
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Intent i = new Intent(this, ProgramDetailsActivity.class);
        
        AppEntry app = (AppEntry) items.get(position).getDataObject();
        
        i.putExtra(ProgramListActivity.APP_ID, app.appId);
        startActivity(i);
    }
    
    /**
     * 
     */
    @Override
    protected void onPause() {
        super.onPause();
        db.close();
        db = null;
    }

    /**
     * 
     */
    @Override
    protected void onResume() {
        super.onResume();
        if (db == null) {
            db = new DbHelper(this);
        }
    }
    
    /**
     * 
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return CommonMenu.createMenu(this, menu);
    }

    /**
     * 
     */
    @Override
    public boolean onOptionsItemSelected(Menu.Item item) {
        return CommonMenu.handleOption(item);
    }
    
}
